<?php
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
/*
require('/home/wap247/public_html/9gag/includes/functions.php');
require('/home/wap247/public_html/9gag/includes/simple_html_dom.php');
*/
require('D:/Projects/gagcrawler/includes/functions.php');
require('D:/Projects/gagcrawler/includes/simple_html_dom.php');
$url = 'http://dongcam.vn/c/thugian';

echo '<pre>';
function getDongcamPost($url){
	// create a new cURL resource
	$ch = curl_init();

	$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
	$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
	$header[] = "Cache-Control: max-age=0";
	$header[] = "Connection: keep-alive";
	$header[] = "Keep-Alive: 300";
	$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
	$header[] = "Accept-Language: en-us,en;q=0.5";
	$header[] = "Pragma: "; // browsers keep this blank.
	// set URL and other appropriate options
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	//curl_setopt($ch, CURLOPT_PROXY, '210.245.0.24:80');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	// grab URL and pass it to the browser
	$responseContent = curl_exec($ch);
	// close cURL resource, and free up system resources
	curl_close($ch);

	$html = str_get_html($responseContent);
	$type = 'photo';
	$postTitle = '';
	$postImage = '';
	foreach($html->find('article.post') as $element){
		$aTag = $element->find('a', 0);
		$imgTag = $element->find('img.size-full', 0);
		if(!strstr($imgTag->src, 'http://')){
			$imgTag->src = 'http://dongcam.vn'.$imgTag->src;
		}
		$iframeTag = $element->find('iframe.youtube-player', 0);

		if(trim($aTag->href) != '' && trim($imgTag->src) != ''){
			if($iframeTag->src != ''){
				$type = 'video';
				$videoCode = current(explode('?', $iframeTag->src));
				$imgTag->src = 'http://img.youtube.com/vi/'.end(explode('/', $videoCode)).'/0.jpg';
			}
			else{
				$type = 'photo';
				$videoCode = '';
			}
			/*
			if($iframeTag->src == '') continue;
			if($type == 'video') die($videoCode.'->'.$imgTag->src);
			*/
			
			$unique = md5($imgTag->src);
			echo "\tcrawling [".end(explode('/',$aTag->href))."] .. ";
			$postTitle = mysql_real_escape_string(strip_tags($aTag->innertext));
			$postUrl = mysql_real_escape_string($aTag->href);
			//$postImage = mysql_real_escape_string($postImage);
			$fileInfo = imageDownloader($imgTag->src);
			if(!$fileInfo) continue;
			$postImage = $fileInfo['name'];
			$parse = parse_url($aTag->href);
			$sqlInsert = "INSERT INTO data(original_url, post_title, post_image, post_unique, crawl_date, file_checksum,item_type,video_code,source_page) VALUES('".$postUrl."', '$postTitle', '$postImage', '$unique', NOW(), '".$fileInfo['checksum']."', '$type', '$videoCode', '".$parse['host']."')";
			#$sqlInsert = mysql_real_escape_string($sqlInsert);
			
			if(mysql_query($sqlInsert)){
				echo "\tdone\n";
			}
			else{
				echo "\tfailed -> \n".mysql_error()."\n\n";
				die("\n-------BREAK-------\n");
			}
			
		}
	}

}
require('D:/Projects/gagcrawler/dbconnect.php');
//require('/home/wap247/public_html/9gag/dbconnect.php');
#echo '<ol>';
$numPage = 2;
for($i = 1; $i <=$numPage; $i++){
	#echo '<li>';
	echo "----------------------\nCrawling page $i/$numPage:\n";
	if($i == 1){
		getDongcamPost($url);
	}
	else{
		getDongcamPost($url.'/page/'.$i);
	}
	sleep(5);
	#echo '</li>';
}
echo "\n----------------------[DONE]----------------------\n";
#echo '<ol>';
